package com.molichuxing.services.business.service.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.services.business.convertor.Convert;
import com.molichuxing.services.business.dto.response.*;
import com.molichuxing.services.business.service.CarFenceExceptionBizService;
import com.molichuxing.services.infrastructure.dto.response.*;
import com.molichuxing.services.infrastructure.dto.response.CarDto;
import com.molichuxing.services.infrastructure.service.*;
import com.molichuxing.services.property.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 车辆围栏异常组合服务类
 * </p>
 * 
 * @author xuelin yi
 */
@Service("carFenceExceptionBizService")
public class CarFenceExceptionBizServiceImpl implements CarFenceExceptionBizService {

	private static final Logger logger = LoggerFactory.getLogger(CarFenceExceptionBizServiceImpl.class);

	@Resource
	private CarFenceExceptionService carFenceExceptionService;

	@Resource
	private CarService carService;

	@Override
	public Paged<CarFenceExceptionBizDto> getCarsFenceExceptionPaged(Map<String, Object> mapParam, Integer pageNum, Integer pageSize) {
		Paged<CarFenceExceptionBizDto> paged = new Paged<>();
		//paged.setTab(getTabList(Integer.parseInt(mapParam.get("type").toString())));
		if (null != mapParam.get("licencePlate") || mapParam.get("vin") != null) {
			String licencePlate = mapParam.get("licencePlate") == null ? null : mapParam.get("licencePlate").toString();
			String vin = mapParam.get("vin") == null ? null : mapParam.get("vin").toString();
			CarDto carDto = carService.getByVinLicencePlate(vin, licencePlate);
			if (carDto == null) {
				return paged;
			}
			mapParam.put("carId", carDto.getId());
		}
		Paged<FenceExceptionDto> result = carFenceExceptionService.getFenceExceptionPage(mapParam, pageNum, pageSize);
		if ((null == result) || (null == result.getList())) {
			return paged;
		}
		Map<Integer, CarDto> carMap = carService.getCarByIds(getCarIdList(result.getList()));
		paged.setList(Convert.toCarFenceExceptionBizDto(result.getList(), carMap));
		paged.setTotal(result.getTotal());
		paged.setPageSize(pageSize);
		return paged;
	}

	@Override
	public CarFenceExceptionBizDto getCarsFenceExceptionDetail(Integer fenceExceptionId) {
		FenceExceptionDto fenceExceptionDto = carFenceExceptionService.getCarsFenceExceptionDetail(fenceExceptionId);
		if(null == fenceExceptionDto) {
			return null;
		}
		Map<Integer, CarDto> carMap = carService.getCarByIds(Arrays.asList(fenceExceptionDto.getCarId()));
		return Convert.toCarFenceExceptionBizDto(fenceExceptionDto, carMap);
	}


	private List<Integer> getCarIdList(List<FenceExceptionDto> fenceExceptionDtoList) {
		if((null == fenceExceptionDtoList) || (fenceExceptionDtoList.size() == 0)) {
			return null;
		}
		List<Integer> carIds = new ArrayList<>();
		fenceExceptionDtoList.forEach(data ->{
			carIds.add(data.getCarId());
		});
		return carIds;
	}

}
